﻿using System;
using System.Collections.Generic;
using System.Web;
using umbraco.presentation.umbracobase.library;
using umbraco.cms.businesslogic.web;
using System.Web.Security;

namespace uForum.Library {
    public class Rest {
        
        //public int _currentMember = umbraco.presentation.umbracobase.library.library.CurrentMemberId();
        public static string HasAccess(int forumId) {
            string retval = "Not Logged On";

            int _currentMember = umbraco.presentation.umbracobase.library.library.CurrentMemberId();

            if (_currentMember > 0){
                retval = "IsLoggedOn";

            MembershipUser member = Membership.GetUser(_currentMember);
            retval += " AS " + member.Email;
            }

            if (Access.HasAccces(forumId, _currentMember))
                retval += " and HasAccess";

            return retval;
        }

        public static string NewTopic(int forumId) {
            umbraco.presentation.nodeFactory.Node n = new umbraco.presentation.nodeFactory.Node(forumId);
            int _currentMember = umbraco.presentation.umbracobase.library.library.CurrentMemberId();

            if (_currentMember > 0 && n != null && Access.HasAccces(n.Id, _currentMember)) {
                string title = HttpContext.Current.Request["title"];
                string body = HttpContext.Current.Request["body"];

                Businesslogic.Topic t = Businesslogic.Topic.Create(forumId, Utills.Sanitize(title), Utills.Sanitize(body), _currentMember);

                return Library.Xslt.NiceTopicUrl(t.Id);
            } else {
                return "0";
            }
        }

        public static string NewComment(int topicId, int itemsPerPage) {
            int _currentMember = umbraco.presentation.umbracobase.library.library.CurrentMemberId();

            if (_currentMember > 0 && topicId > 0) {
                string body = HttpContext.Current.Request["body"];
                Businesslogic.Comment c = Businesslogic.Comment.Create(topicId, Utills.Sanitize(body), _currentMember);

                return Xslt.NiceCommentUrl(c.TopicId, c.Id, itemsPerPage);
            }

            return "";
        }

    }
}
